﻿using LJX.SmartPark.Common.Extensions;
using LJX.SmartPark.Common.ResponseOutput;
using LJX.SmartPark.Domain.Area;
using LJX.SmartPark.Service.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.ConstrainedExecution;
using System.Text;
using System.Threading.Tasks;

namespace LJX.SmartPark.Service.Area
{
    public class ProvisionalPayService : IProvisionalPayService
    {
        private readonly IRepository<ProvisionalPay> _studentService;

        public ProvisionalPayService(IRepository<ProvisionalPay> provisionalPay)
        {
            _studentService = provisionalPay;
        }

        public async Task<Response<List<ProvisionalPay>>> GetProvisionalPayAsync(ProvisionalPayForm monitor)
        {
            var students = await _studentService.GetAllListAsync();
            if (!string.IsNullOrEmpty(monitor.orderNum))
            {
                students = students.Where(x => x.OrderNum.Contains(monitor.orderNum)).ToList();
            }
            if (!string.IsNullOrEmpty(monitor.carNum))
            {
                students = students.Where(x => x.CarNum.Contains(monitor.carNum)).ToList();
            }
            if (monitor.startTime != null && monitor.endTime != null)
            {
                //monitor.startTime = monitor.startTime.AddHours(8);
                //monitor.endTime = monitor.endTime.AddHours(8);
                students = students.Where(x => x.OrderDate >= monitor.startTime && x.OrderDate <= monitor.endTime).ToList();
            }
            var count = students.Count();
            students = students.Skip((monitor.pageIndex - 1) * monitor.pageSize).Take(monitor.pageSize).ToList();
            var result = students.MapToList<ProvisionalPay>();
            return Response<List<ProvisionalPay>>.Ok(result, count);
        }

        public Response<ProvisionalPay> GetProvisionalPayById(ProvisionalPay input)
        {
            var student = _studentService.GetListQuery().FirstOrDefault(m => m.Id == input.Id);
            var result = student.MapTo<ProvisionalPay>();
            return Response<ProvisionalPay>.Ok(result);
        }
    }


}
